package api

import (
	"github.com/gin-gonic/gin"
	"github.com/zhengchalei/gadmin/framework"
	"github.com/zhengchalei/gadmin/project/system/model"
	"github.com/zhengchalei/gadmin/project/system/service"
)

func UserAll(c *gin.Context) {
	data := service.UserFindAll()
	framework.Data(c, data)
}

func UserPage(c *gin.Context) {
	params := &model.SysUser{}
	queryPage := &framework.ReqPage{}
	if err := c.ShouldBind(params); err != nil {
		framework.Error(c, err)
		return
	}
	if err := framework.QueryPage(c, queryPage); err != nil {
		framework.Error(c, err)
		return
	}
	page, count := service.UserFindPage(params, queryPage)
	framework.Page(c, page, count)
}

func UserSave(c *gin.Context) {
	user := &model.SysUser{}
	if err := c.ShouldBind(user); err != nil {
		framework.Error(c, err)
		return
	}
	if err := service.UserSave(user); err != nil {
		framework.Error(c, err)
		return
	}
	framework.Data(c, user)
}

func UserUpdate(c *gin.Context) {
	user := &model.SysUser{}
	if err := c.ShouldBind(user); err != nil {
		framework.Error(c, err)
		return
	}
	if err := service.UserUpdate(user); err != nil {
		framework.Error(c, err)
		return
	}
	framework.Data(c, user)
}

func UserDelete(c *gin.Context) {
	id := c.Query("id")
	if id == "" {
		framework.Fail(c, "参数有误")
		return
	}
	if err := service.UserDelete(id); err != nil {
		framework.Error(c, err)
		return
	}
	framework.Ok(c)
}
